package com.hdoit.kylin.web.proposal.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.kylin.logical.proposal.model.Proposal;
import com.hdoit.kylin.logical.proposal.service.IProposalService;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.viewmodel.ProposalSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.DaoBaseException;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;

@Controller
@RequestMapping(value="/admin/proposal")
public class ProposalControllers extends BaseController{

	@Override
	public String getRoutePath() {
		return "areas/admin/proposal/";
	}

	private IProposalService proposalService;
	public IStudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	private IStudentService studentService;
	private String role;
	private String groupId;
	
	public IProposalService getProposalService() {
		return proposalService;
	}

	@Resource
	public void setProposalService(IProposalService proposalService) {
		this.proposalService = proposalService;
	}
	
	private final String indexViewName = "index";
	private final String saveViewName = "save";
	private final String outputViewName = "output";
	private final String outputSonViewName = "outputSon";
	private final String detailedViewName = "proposalDetailed";
	private final String detailedVersionViewName = "proposalVersionDetailed";
	private final String loadProposalViewName = "loadProposal";
	private final String saveVersionViewName = "saveVersion";


	
	@RequestMapping(value = indexViewName,method = {RequestMethod.GET,RequestMethod.POST})
	public String index(Model model, ProposalSearch search, boolean ajaxPost) throws DaoBaseException{
		role = proposalService.checkRole(getLoginModel().getLoginId());
//		if("teacher".equals(role)){
//			groupId = teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId());
//			pagerModel.setTotalAmount(proposalService.queryCount(search,getLoginModel().getLoginId(),role));
//		}else if("student".equals(role)){
//			pagerModel.setTotalAmount(proposalService.queryCount(search,getLoginModel().getLoginId(),role));
//		}
		List<Proposal> proposals = proposalService.queryListByLoginId(getLoginModel().getLoginId(),groupId,role);
		
		model.addAttribute("proposals", proposals);
		model.addAttribute("proposalSearch", search);
		
		if(ajaxPost) {
			return view(outputViewName);
		}
		return view(indexViewName);
	}
	
	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET })
	public String proposalDetailed(Model model,String key){
		Proposal proposal;
		if(StringUtils.isEmpty(key)){
			proposal=new Proposal();
		}
		else{
			proposal=proposalService.queryModel(key);
		}
		
		model.addAttribute("proposal", proposal);
		
		return view(detailedViewName);
	}
	
	@RequestMapping(value = detailedVersionViewName, method = { RequestMethod.GET })
	public String proposalVersionDetailed(Model model,String key){
		Proposal proposal;
		if(StringUtils.isEmpty(key)){
			//加一个弹窗提示没选择开题报告
			proposal=new Proposal();
			
			model.addAttribute("proposal", proposal);
			
			return view(detailedViewName);
		}
		else{
			proposal=proposalService.queryModel(key);
		}
		
		model.addAttribute("proposal", proposal);
		
		return view(detailedVersionViewName);
	}
	
	@RequestMapping(value=saveVersionViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String saveVersion(Model model,Proposal proposal,String proposalName){
		role = proposalService.checkRole(getLoginModel().getLoginId());
		if("student".equals(role)){
			groupId = studentService.queryGroupIdByLoginId(getLoginModel().getLoginId());
		}
		
		if(StringUtils.isEmpty(proposal.getProposalId())){
			proposal.setProposalName(proposalName);
			proposal.setProposalId(CommonUtils.getKeyId());
			proposal.setCreateUserId(getLoginModel().getLoginId());
			proposal.setCreateDate(DateUtils.getCurrent());
			proposal.setGroupId(groupId);
			proposal.setMainMark(0);
			proposal.setModelStatus(ModelStatus.add);
		}
		
		ActionResult actionResult;
		if(proposalService.applyChange(proposal)){
			actionResult=new ActionResult();
		}
		else{
			actionResult=new ErrorResult();
		}
		
		model.addAttribute("proposal", proposal);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		
		return view(detailedVersionViewName);
	}
	
	@RequestMapping(value=saveViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String save(Model model,Proposal proposal){
		role = proposalService.checkRole(getLoginModel().getLoginId());
		if("student".contains(role)){
			groupId = studentService.queryGroupIdByLoginId(getLoginModel().getLoginId());
		}
		
		if(StringUtils.isEmpty(proposal.getProposalId())){
			proposal.setProposalId(CommonUtils.getKeyId());
			proposal.setCreateUserId(getLoginModel().getLoginId());
			proposal.setCreateDate(DateUtils.getCurrent());
			proposal.setGroupId(groupId);
			proposal.setMainMark(1);
			proposal.setModelStatus(ModelStatus.add);
		}
//		else{
//			proposal.setModelStatus(ModelStatus.update);
//		}
		
		ActionResult actionResult;
		if(proposalService.applyChange(proposal)){
			actionResult=new ActionResult();
		}
		else{
			actionResult=new ErrorResult();
		}
		
		model.addAttribute("proposal", proposal);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		
		return view(detailedViewName);
	}
	
	@RequestMapping(value = loadProposalViewName,method = {RequestMethod.GET,RequestMethod.POST})
	public String loadProposal(String proposalName,Model model, PagerModel pagerModel, ProposalSearch search, boolean ajaxPost) throws DaoBaseException{
		role = proposalService.checkRole(getLoginModel().getLoginId());
		
		pagerModel.setTotalAmount(proposalService.queryCountByProposalName(proposalName,getLoginModel().getLoginId(),role));
		List<Proposal> proposalSons = proposalService.queryListByProposalName(proposalName);
		
		System.out.println("1111111111111111");
		System.out.println(proposalName);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("proposalSons", proposalSons);
		model.addAttribute("proposalSearch", search);
		
		return view(outputSonViewName);
	}
}
